using UnityEngine;

public class BackgroundRepeat : MonoBehaviour
{
	public SpriteRenderer[] renders = new SpriteRenderer[0];

	private RenderBounds rectRender = default(RenderBounds);

	private Camera gameCamera;

	private int p;

	private float leftBorder;

	private float rightBorder;

	private Vector3[] startPosition = new Vector3[0];

	private float[] slidePosition = new float[0];

	private float asd;

	private void OnDrawGizmos()
	{
		if (startPosition.Length == 0)
		{
			rectRender = GetRendererBounds(renders);
			Gizmos.DrawLine(new Vector3(rectRender.left, -5f, 0f), new Vector3(rectRender.left, 5f, 0f));
			Gizmos.DrawLine(new Vector3(rectRender.right, -5f, 0f), new Vector3(rectRender.right, 5f, 0f));
			gameCamera = Camera.main;
			leftBorder = GetLeftBorder(gameCamera);
			rightBorder = GetRightBorder(gameCamera);
			Gizmos.DrawLine(new Vector3(leftBorder, -5f, 0f), new Vector3(leftBorder, 5f, 0f));
			Gizmos.DrawLine(new Vector3(rightBorder, -5f, 0f), new Vector3(rightBorder, 5f, 0f));
		}
	}

	private void Start()
	{
		gameCamera = Camera.main;
		rectRender = GetRendererBounds(renders);
		startPosition = new Vector3[renders.Length];
		slidePosition = new float[renders.Length];
		for (p = 0; p < renders.Length; p++)
		{
			startPosition[p] = renders[p].transform.localPosition;
			slidePosition[p] = rectRender.right - GetRightBorder(renders[p]);
		}
	}

	private void Update()
	{
		for (p = 0; p < renders.Length; p++)
		{
			asd = rectRender.dist * Mathf.Floor((GetLeftBorder(gameCamera) + slidePosition[p]) / rectRender.dist);
			asd = Mathf.Floor(asd * 100f) / 100f;
			renders[p].transform.localPosition = new Vector3(startPosition[p].x + asd, startPosition[p].y, startPosition[p].z);
		}
	}

	private RenderBounds GetRendererBounds(SpriteRenderer[] renders)
	{
		float num = 0f;
		float num2 = 0f;
		for (p = 0; p < renders.Length; p++)
		{
			float a = num;
			Vector3 position = renders[p].transform.position;
			float x = position.x;
			Vector3 min = renders[p].sprite.bounds.min;
			num = Mathf.Min(a, x + min.x);
			float a2 = num2;
			Vector3 position2 = renders[p].transform.position;
			float x2 = position2.x;
			Vector3 max = renders[p].sprite.bounds.max;
			num2 = Mathf.Max(a2, x2 + max.x);
		}
		return new RenderBounds(num, num2);
	}

	private float GetLeftBorder(Camera gameCamera)
	{
		Vector3 position = gameCamera.transform.position;
		return position.x - gameCamera.orthographicSize * gameCamera.aspect;
	}

	private float GetRightBorder(Camera gameCamera)
	{
		Vector3 position = gameCamera.transform.position;
		return position.x + gameCamera.orthographicSize * gameCamera.aspect;
	}

	private float GetRightBorder(SpriteRenderer render)
	{
		Vector3 position = render.transform.position;
		float x = position.x;
		Vector3 max = render.sprite.bounds.max;
		return x + max.x;
	}
}
